#include "bsp/lcd.hpp"
#include "hal/delay.hpp"
#include "hal/gpio.hpp"
#include "hal/i2c.hpp"
#include "hal/register.hpp"
#include "hal/spi.hpp"
#include "hal/timer.hpp"
#include "hal/adc.hpp"
#include "ti_msp_dl_config.h"
#include "bsp/touch.hpp"
#include "arm_math.h"

using lcd = bsp::lcd<hal::delay::ms, bsp::detail_lcd::DMAConfigType::enable>;
using touch = bsp::touch;
// 测试颜色数据（RGB565格式）
unsigned int adc_value[200] = {};
unsigned int adc_value1 = 0;
unsigned int adc_value2 = 0;
unsigned int adc_value3 = 0;
unsigned int adc_value4 = 0;
unsigned int adc_value5 = 0;
uint8_t color_data[2500] = {};
static uint8_t init_flag = 0;
static int32_t x_touch[10];
static int32_t y_touch[10];
static uint16_t td = 0;
uint16_t gADCSamples[1000];
//读取ADC的数据

bool gCheckADC = false;

void adc_start()
{
    DL_ADC12_startConversion(ADC_0_INST);
}

void adc_stop()
{
    DL_ADC12_stopConversion(ADC_0_INST);
}

void adc_disable()
{
    DL_ADC12_disableConversions(ADC_0_INST);
}

void adc_enable()
{
    DL_ADC12_enableConversions(ADC_0_INST);
}

void adc_reset()
{
    DL_ADC12_enableConversions(ADC_0_INST);
    DL_ADC12_startConversion(ADC_0_INST);
}

unsigned int adc_getValue_1()
{
    // DL_ADC12_enableConversions(ADC_0_INST);
    // while (false == gCheckADC)
    // {
    //     __WFE();
    // }
    uint16_t gAdcResult = DL_ADC12_getMemResult(ADC_0_INST, DL_ADC12_MEM_IDX_0);
    gCheckADC = false;

    return gAdcResult;
}
unsigned int adc_getValue_2()
{
    // DL_ADC12_enableConversions(ADC_0_INST);
    // while (false == gCheckADC)
    // {
    //     __WFE();
    // }
    uint16_t gAdcResult = DL_ADC12_getMemResult(ADC_0_INST, DL_ADC12_MEM_IDX_1);
    gCheckADC = false;

    return gAdcResult;
}


int main()
{
    SYSCFG_DL_init();
    adc_start();
    // NVIC_EnableIRQ(ADC_0_INST_INT_IRQN);
    // gCheckADC = false;
    // lcd::init();
    // NVIC_EnableIRQ(GPIOB_INT_IRQn);
    //  init_flag = touch::init();
    // lcd::clear(0x0055);
    // lcd::flush(10, 10, 20, 50, color_data);
    // bsp::touch_soft::ft6336_rest();
    // td=bsp::touch_soft::ft6336_get_td_status();
    /* Configure DMA source, destination and size */
    //****************************************************************//
    // DL_DMA_setSrcAddr(DMA, DMA_CH1_CHAN_ID,(uint32_t)0x40556280);
    //
    // DL_DMA_setDestAddr(DMA, DMA_CH1_CHAN_ID, reinterpret_cast<uint32_t>(&gADCSamples[0]));
    // //初始化里一次DMA传输大小没设置再设置
    // // DL_DMA_setTransferSize(DMA, DMA_CH0_CHAN_ID, 1000);
    // DL_DMA_enableChannel(DMA, DMA_CH1_CHAN_ID);
    // /* Setup interrupts on device */
    // NVIC_EnableIRQ(ADC_0_INST_INT_IRQN);
    // DL_ADC12_startConversion(ADC_0_INST);
    //****************************************************************//
    // hal::adc0_0::start_adc_dma(reinterpret_cast<uint32_t>(&gADCSamples[0]));
    hal::adc0_0::start_adc_dma_fifo(reinterpret_cast<uint32_t>(&gADCSamples[0]));
    while (1)
    {



    }
}
